package com.dcxm.stu.service.impl

import com.dcxm.stu.api.HomeWorkInfo
import com.dcxm.stu.bean.HomeWork
import com.dcxm.stu.dao.impl.HomeWorkDaoI
import com.dcxm.stu.service.HomeWorkService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.commons.CommonsMultipartFile
import java.io.File
import java.util.*

@Service
class HomeWorkServiceI : HomeWorkService {

    @Autowired
    private lateinit var homeWorkDao: HomeWorkDaoI

    /**
     * @author youngxhui
     * 获取所有作业列表
     */
    override fun listHomeWork(): HomeWorkInfo {
        val homeList = homeWorkDao.listHomeWork()
        var state = "201"
        var text = "没有数据"
        if (homeList.isNotEmpty()) {
            state = "200"
            text = "获取成功"
        }
        return HomeWorkInfo(state, text, homeList)
    }

    /**
     * 添加作业
     * @author youngxhui
     * <p>未完成</p>
     */
    override fun insertHomeWork(homeWork: HomeWork, files: Array<CommonsMultipartFile>,
                                path: String): HomeWorkInfo {
        for (file in files) {
            if (file.isEmpty) continue
            val type = file.originalFilename.substring(file.originalFilename.indexOf('.'))
            val fileName = UUID.randomUUID().toString() + type
            val newPath = File(path + fileName)
            file.transferTo(newPath)
        }
        var state = "100"
        var text = "插入错误"
        // 多图地址如何存取？ 写入数据库
        val flag = homeWorkDao.insertHomeWork(homeWork, path = String())
        if (flag == 1) {
            state = "200"
            text = "插入成功"
        }
        return HomeWorkInfo(state, text, null)
    }

    /**
     * @param id 要删除的 id
     * @return Homework 返回的 homework json
     */
    override fun deleteHomeWork(id: String): HomeWorkInfo {
        var state = "100"
        var text = "删除失败"
        val flag = homeWorkDao.deleteHomeWork(id)
        if (flag == 1) {
            state = "200"
            text = "删除成功"
        }
        return HomeWorkInfo(state, text, null)
    }

    override fun listSelectHomeWorkById(idList: Array<String>): HomeWorkInfo {
        if (idList.isEmpty()) return HomeWorkInfo("101", "无效参数", null)
        val homeWorkList = idList.map { homeWorkDao.listSelectHomeWorkById(it) }
        return if (homeWorkList.isEmpty()) HomeWorkInfo("201", "没有数据", null)
        else HomeWorkInfo("200", "获取成功", homeWorkList)
    }

    override fun listSelectHomeWorkByType(type: String): HomeWorkInfo {
        val homeWorkList = homeWorkDao.listSelectHomeWorkByType(type)
        return if (homeWorkList.isNotEmpty()) HomeWorkInfo("200", "获取成功", homeWorkList)
        else HomeWorkInfo("201", "没有数据", null)
    }

    override fun updateHomeWork(homeWork: HomeWork): HomeWorkInfo {
        val id = homeWorkDao.updateHomeWork(homeWork)
        return if (id != 0) HomeWorkInfo("200", "更新成功", null)
        else HomeWorkInfo("201", "没有数据", null)
    }
}